<?php
/**
 * @author dinglin
 * 导入2012-07-01之后到数据
 * */
apf_require_class('Oa_Job_Attendance_Abstract');
apf_require_class('Const_LeaveType');
apf_require_class('Bll_Attendance_AccessDayRecord');
class Oa_Job_Attendance_OldDayRecordImport extends Oa_Job_Attendance_Abstract {

    /** 考勤系统读 **/
    private $readdb = "attendance_slave";
    /** 考勤系统写 **/
    private $writedb = "attendance_master";
    //今天
    private $date = '';
    //sql
    private $sql = '';
    //上班时间
    private $am_start_work = 900;
    private $am_late_time = 910;
    private $am_absenteeism_time = 930;
    private $am_end_work = 1200;
    private $pm_start_work = 1400;
    private $pm_late_time = 1410;
    private $pm_absenteeism_time = 1430;
    private $pm_leave_early_time = 1730;
    private $pm_end_work = 1800;
    public function run($start,$end){
        $bll = new Bll_Attendance_AccessDayRecord();
        $all_users = $this->_get_all_users();
        $start=strtotime($start);
        $end=strtotime($end);
        while($start<=$end){
            $this->date = date('Ymd',$start);
            $start = $start + 24*60*60;
            $this->log("开始导入".date('Y-m-d',$start)."日打卡记录");
            //$this->delete_data();
            //当天所有打卡记录
            $all_records = $this->_get_all_day_record();
            $all_records = $this->_do_data_to_index_id($all_records);

            foreach($all_users as $user){
                $insert_data=array();
                $insert_data['user_id'] = $user['user_id'];
                $insert_data['dateline'] = $this->date;
                if(isset($all_records[$user['user_id']])){
                    $res = $bll->check_day_work_status($this->date,$all_records[$user['user_id']]['start_work'],$all_records[$user['user_id']]['end_work']);
                    $insert_data['am_status'] = $res[0];
                    $insert_data['pm_status'] = $res[1];
                    //迟到
                    if($res[0] == Const_LeaveType::KQ_AM_STATUS_1){
                        $insert_data['is_later'] = 1;
                    }
                    //早退
                    if($res[1] == Const_LeaveType::KQ_PM_STATUS_2){
                        $insert_data['leave_early'] = 1;
                    }
                }
                $this->_insert_user_day_status($insert_data);
            }
            $this->log("导入".date('Y-m-d',$start)."日打卡记录完毕");
            unset($all_records);
        }
        unset($all_users);
    }
    private function delete_data(){
        $this->sql = "delete from user_day_status where dateline=".$this->date;
        $this->execute( $this->sql,array(), $this->writedb);
    }
    /**
     * 插入员工到初始记录
     * @param unknown_type $insert_data
     */
    private function _insert_user_day_status($data){
        $this->sql = "INSERT INTO user_day_status (user_id,dateline,days,am_status,am_type,am_leave_status,pm_status,pm_type,pm_leave_status,is_later,leave_early) values (%s)";
        //以下准备数据
        $insert_data = array(
                'user_id'        => $data['user_id'],
                'dateline'       => $data['dateline'],
                'days'           => isset( $data['days'] ) ? $data['days'] : 0.0,
                'am_status'      => isset( $data['am_status'] ) ? $data['am_status'] : Const_LeaveType::KQ_AM_STATUS_5,
                'am_type'        => isset( $data['am_type'] ) ? $data['am_type'] : 0,
                'am_leave_status'=> isset( $data['am_leave_status'] ) ? $data['am_leave_status'] : 0,
                'pm_status'      => isset( $data['pm_status'] ) ? $data['pm_status'] : Const_LeaveType::KQ_PM_STATUS_6,
                'pm_type'        => isset( $data['pm_type'] ) ? $data['pm_type'] : 0,
                'pm_leave_status'=> isset( $data['pm_leave_status'] ) ? $data['pm_leave_status'] : 0,
                'is_later'       => isset( $data['is_later'] ) ? $data['is_later'] : 0,
                'leave_early'    => isset( $data['leave_early'] ) ? $data['leave_early'] : 0,
        );
        $this->sql = sprintf($this->sql,join(',',$insert_data));
        $this->execute( $this->sql,array(), $this->writedb);
        $this->log("导入date:{$this->date}日的user_id:{$data['user_id']}打卡记录完毕");
        unset($insert_data);
    }
    /**
     * 获取所有在职员工,07-01之前离职的员工去掉
     * @return multitype:
     */
    private function _get_all_users() {
        $this->sql = "SELECT user_id FROM user_info WHERE dimission_date >= '2012-07-01' OR dimission_date < '2000-01-01'";
        return $this->get_db_rows($this->sql,$this->writedb,array());
    }
    /**
     * 获取当天所有打卡记录
     * @return multitype:
     */
    private function _get_all_day_record(){
        $this->sql = "SELECT end_work,start_work,user_id FROM access_day_record WHERE access_date = ".$this->date;
        return $this->get_db_rows($this->sql,$this->writedb,array());
    }
    /**
     * 数据转换为以user_id为索引
     * @param unknown_type $data
     */
    private function _do_data_to_index_id(&$data){
        $result = array();
        foreach($data as $one){
            $result[$one['user_id']] = $one;
        }
        unset($data);
        return $result;
    }
}
